﻿using MyMusicPlay._4._0.Manager;
using MyMusicPlay._4._0.Operate;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;


namespace MyMusicPlay._4._0
{
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 添加歌单
        /// </summary>
        public void LoadBut_MouseUp(object sender, MouseButtonEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
            folderBrowserDialog1.ShowDialog();
            string path = folderBrowserDialog1.SelectedPath;
            if (string.IsNullOrEmpty(path))
                return;

            var name = path.Substring(path.LastIndexOf('\\') + 1);
            Media.AddMusicListToAll(name, path);

            Disposition.AddDispositionOfMusicList(name, path);

            //渲染出来
            Rander.RenderViewOfMusicLists(name);
        }

        /// <summary>
        /// 配置按钮
        /// </summary>
        public void ConfigureBut_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (CurrView == MoveViewEnum.ConfigView)
            {
                MoveView(MoveViewEnum.PlayView);
            }
            else
            {
                Rander.RenderView();
                MoveView(MoveViewEnum.ConfigView);
            }
        }

        /// <summary>
        /// 点击歌单
        /// </summary>
        public void MusicList_MouseUp(object sender, MouseButtonEventArgs e)
        {
            MoveView(MoveViewEnum.PlayView);
            var element = sender as TextBlock;
            var listName = element.Text;

            MusicPlaying.LoadMusisListToDisPlay(listName);
        }

        #region 检索
        /// <summary>
        /// 检索中的歌曲集合
        /// </summary>
        private List<Music> RetrievalMusicList = new List<Music>();

        private bool _Retrieval_Judge = false;
        public bool Retrieval_Judge
        {
            get { return _Retrieval_Judge; }
            set
            {
                if (value != _Retrieval_Judge && value)
                {
                    Animate.Deformation(Retrieval_StackPanel, Animate.direction.Height, Retrieval_StackPanel.Height, 40, 200);
                    Animate.Move(MusicLists_Grid, MusicLists_Grid.Margin, new Thickness(0, 40, 0, 0), 200);
                }
                if (value != _Retrieval_Judge && !value)
                {
                    Animate.Deformation(Retrieval_StackPanel, Animate.direction.Height, Retrieval_StackPanel.Height, 0, 400);
                    Animate.Move(MusicLists_Grid, MusicLists_Grid.Margin, new Thickness(0, 0, 0, 0), 400);
                }
                _Retrieval_Judge = value;
            }
        }
        /// <summary>
        /// 检索 [按钮]
        /// </summary>
        private void Retrieval_MouseUp(object sender, MouseButtonEventArgs e)
        {
            if (CurrView != MoveViewEnum.PlayView)
                return;
            Retrieval_Judge = !Retrieval_Judge;
            //检索关闭的时候,重新刷新渲染
            if (!Retrieval_Judge)
            {
                Rander.RenderViewOfMusicList(Media.CurDisplayList.ToLists());
                RetrievalOfText_TestBox.Text = "";
            }
        }


        private RetrievalOfWhereEnum _RetrievalOfWhere_Judge = RetrievalOfWhereEnum.Name;
        public RetrievalOfWhereEnum RetrievalOfWhere_Judge
        {
            get { return _RetrievalOfWhere_Judge; }
            set
            {
                var arr = typeof(RetrievalOfWhereEnum).GetEnumNames();
                arr.ToList().Remove(_RetrievalOfWhere_Judge.ToString());
                var str = arr[MyRandom.Random.Next(0, arr.Length)];
                _RetrievalOfWhere_Judge = (RetrievalOfWhereEnum)str.GetEnumIndex<RetrievalOfWhereEnum>();
                RetrievalOfWhere_TestBlock.Text = str;
            }
        }
        /// <summary>
        /// 检索的条件
        /// </summary>
        private void RetrievalOfWhere_MouseDown(object sender, MouseButtonEventArgs e)
        {
            RetrievalOfWhere_Judge = RetrievalOfWhere_Judge;
        }

        /// <summary>
        /// 检索开始
        /// </summary>
        private void RetrievalOfText_TextChanged(object sender, TextChangedEventArgs e)
        {
            var str = RetrievalOfText_TestBox.Text.Trim().ToLower();
            var strArr = str.Split(' ');

            RetrievalMusicList.Clear();
            //[这个匹配机制不是理想的]
            foreach (var item in strArr)
                if (item != " ")
                    RetrievalMusicList.AddRange(GetCurRetrievalDisplayList(RetrievalOfWhere_Judge, str));

            //进行渲染
            Rander.RenderViewOfMusicList(RetrievalMusicList);
        }

        private IEnumerable<Music> GetCurRetrievalDisplayList(RetrievalOfWhereEnum retrievalOfWhereEnum, string str) => retrievalOfWhereEnum switch
        {
            RetrievalOfWhereEnum.Name => Media.CurDisplayList.Values.Where(u => u.Name.ToLower().IndexOf(str) >= 0),
            RetrievalOfWhereEnum.Author => Media.CurDisplayList.Values.Where(u => u.Singer.ToLower().IndexOf(str) >= 0),
            _ => new List<Music>()
        };

        public enum RetrievalOfWhereEnum
        {
            Name, Author
        }
        #endregion

        /// <summary>
        /// 显示歌名窗体
        /// </summary>
        private void OpenDisplayView_MouseUp(object sender, MouseButtonEventArgs e)
        {
            var displayWindow = WindowsManage.Find<DisplayMusicWindow>();

            displayWindow.Show();
        }
    }
}
